#pragma once
#include <hiredis/hiredis.h>
#include <thread>
#include <functional>

/////////////////////////////////////////////////////////////////////////////////////////////
/*

    Redis是一个redis集群服务器通信的操作模块
    
*/
class Redis
{
public:
    Redis();
    ~Redis();
    /*············································

                    连接redis
    
    ············································*/
    bool Connect();
    /*············································

            向redis指定的通道channel发布消息
    
    ············································*/
    bool Publish(int channel, std::string message);
    /*············································

            向redis指定的通道subscribe订阅消息
    
    ············································*/
    bool Subscribe(int channel);
    /*············································

        向redis指定的通道unsubscribe取消订阅消息
    
    ············································*/
    bool Unsubscribe(int channel);
    /*············································

            在独立线程中接收订阅通道中的消息
    
    ············································*/
    void ObserverChannelMessage();
    /*············································

          初始化向业务层上报通道消息的回调对象
    
    ············································*/
    void InitNotifyHandler(std::function<void(int, std::string)> fn);

private:
    redisContext* _publish_context;   // hiredis同步上下文对象,负责publish消息
    redisContext* _subscribe_context;  // hiredis同步上下文对象,负责subscribe消息
    // 回调操作,收到订阅消息,给service层上报
    std::function<void(int, std::string)> _notify_message_handler;
};